function testSonar()
	repeat
		write("Front: " .. sonarReading(1), 1)
		write("Right: " .. sonarReading(2), 2)
		write("Left:  " .. sonarReading(3), 3)
	until false
end

function seeObject(direction, distance)
	return sonarReading(direction) < distance
end

function retreatRight()
	backward(A)
	stop(B)
end

function retreatLeft()
	backward(B)
	stop(A)
end

function advance()
	forward(A)
	forward(B)
end

function avoid()
	FRONT = 1
	RIGHT = 2
	LEFT = 3
	DISTANCE = 50

	objFront = seeObject(FRONT, DISTANCE)
	objRight = seeObject(RIGHT, DISTANCE)
	objLeft  = seeObject(LEFT, DISTANCE)
	if(objFront) then
		if (sonarReading(LEFT) > sonarReading(RIGHT)) then
			retreatLeft()
		else
			retreatRight()
		end	
	elseif(objRight or objLeft) then
		advance()
	end
end

repeat
	avoid()
until isPressed(ESCAPE)



